
package com.mephi.model.profile;


import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import com.mephi.interfaces.Profile;


/**
 * A simple default implementation which uses thread safe container in its implementation.
 * 
 * @author mephisto
 * 1.0 2011-05-28
 */
public class ConcurrentProfileMapImpl implements com.mephi.interfaces.ConcurrentMap<String, Profile>
{
	/**
     * Creates a new, empty map with a default initial capacity (16),
     * load factor (0.75) and concurrencyLevel (16).
     */
	public ConcurrentProfileMapImpl()
	{
		profileMap = new ConcurrentHashMap<String, Profile>();
	}
	
	/**
     * Removes all of the mappings from this map.
     */
	@Override
	public void clear()
	{
		profileMap.clear();
	}

	/**
    * Returns an enumeration of the values in this table.
    *
    * @return an enumeration of the values in this table
    * @see #values()
    */
	@Override
	public Enumeration<Profile> elements()
	{
		return profileMap.elements();
	}
	
	/**
    * Returns <tt>true</tt> if this map contains no key-value mappings.
    *
    * @return <tt>true</tt> if this map contains no key-value mappings
    */
	@Override
	public boolean isEmpty()
	{
		return profileMap.isEmpty();
	}

	/**
     * Returns a {@link Set} view of the keys contained in this map.
     * The set is backed by the map, so changes to the map are
     * reflected in the set, and vice-versa.  The set supports element
     * removal, which removes the corresponding mapping from this map,
     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
     * operations.  It does not support the <tt>add</tt> or
     * <tt>addAll</tt> operations.
     *
     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
     * that will never throw {@link java.util.ConcurrentModificationException},
     * and guarantees to traverse elements as they existed upon
     * construction of the iterator, and may (but is not guaranteed to)
     * reflect any modifications subsequent to construction.
     */
	@Override
	public Set<String> keySet()
	{
		return profileMap.keySet();
	}

	/**
    * Returns an enumeration of the keys in this table.
    *
    * @return an enumeration of the keys in this table
    * @see #keySet()
    */
	@Override
	public Enumeration<String> keys()
	{
		return profileMap.keys();
	}

	/**
     * Maps the specified key to the specified value in this table.
     * Neither the key nor the value can be null.
     *
     * <p> The value can be retrieved by calling the <tt>get</tt> method
     * with a key that is equal to the original key.
     *
     * @param key key with which the specified value is to be associated
     * @param game value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
     * @throws NullPointerException if the specified key or value is null
     */
	@Override
	public Profile put(String key, Profile game)
	{
		return profileMap.put(key, game);
	}
	
	/**
     * {@inheritDoc}
     *
     * @return the previous value associated with the specified key,
     *         or <tt>null</tt> if there was no mapping for the key
     * @throws NullPointerException if the specified key or value is null
     */
	@Override
	public Profile putIfAbsent(String key, Profile value)
	{
		return profileMap.putIfAbsent(key, value);
	}

	/**
     * Returns the number of key-value mappings in this map.  If the
     * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
     * <tt>Integer.MAX_VALUE</tt>.
     *
     * @return the number of key-value mappings in this map
     */
	@Override
	public int size()
	{
		return profileMap.size();
	}

	/**
     * Returns a {@link Collection} view of the values contained in this map.
     * The collection is backed by the map, so changes to the map are
     * reflected in the collection, and vice-versa.  The collection
     * supports element removal, which removes the corresponding
     * mapping from this map, via the <tt>Iterator.remove</tt>,
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
     * <tt>retainAll</tt>, and <tt>clear</tt> operations.  It does not
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
     *
     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
     * that will never throw {@link java.util.ConcurrentModificationException},
     * and guarantees to traverse elements as they existed upon
     * construction of the iterator, and may (but is not guaranteed to)
     * reflect any modifications subsequent to construction.
     */
	public Collection<Profile> values()
	{
		return profileMap.values();
	}
	
	/**
     * Tests if the specified object is a key in this table.
     *
     * @param  key   possible key
     * @return <tt>true</tt> if and only if the specified object
     *         is a key in this table, as determined by the
     *         <tt>equals</tt> method; <tt>false</tt> otherwise.
     * @throws NullPointerException if the specified key is null
     */
	@Override
	public boolean containsKey(Object key)
	{
		return profileMap.containsKey(key);
	}

	/**
     * Returns <tt>true</tt> if this map maps one or more keys to the
     * specified value. Note: This method requires a full internal
     * traversal of the hash table, and so is much slower than
     * method <tt>containsKey</tt>.
     *
     * @param value value whose presence in this map is to be tested
     * @return <tt>true</tt> if this map maps one or more keys to the
     *         specified value
     * @throws NullPointerException if the specified value is null
     */
	@Override
	public boolean containsValue(Object value)
	{
		return profileMap.containsValue(value);
	}

	/**
     * Returns a {@link Set} view of the mappings contained in this map.
     * The set is backed by the map, so changes to the map are
     * reflected in the set, and vice-versa.  The set supports element
     * removal, which removes the corresponding mapping from the map,
     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
     * operations.  It does not support the <tt>add</tt> or
     * <tt>addAll</tt> operations.
     *
     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
     * that will never throw {@link java.util.ConcurrentModificationException},
     * and guarantees to traverse elements as they existed upon
     * construction of the iterator, and may (but is not guaranteed to)
     * reflect any modifications subsequent to construction.
     */
	@Override
	public Set<java.util.Map.Entry<String, Profile>> entrySet()
	{
		return profileMap.entrySet();
	}
	
	/**
     * Returns the value to which the specified key is mapped,
     * or {@code null} if this map contains no mapping for the key.
     *
     * @throws NullPointerException if the specified key is null
     */
	@Override
	public Profile get(Object key)
	{
		return profileMap.get(key);
	}

	/**
     * Copies all of the mappings from the specified map to this one.
     * These mappings replace any mappings that this map had for any of the
     * keys currently in the specified map.
     *
     * @param map mappings to be stored in this map
     */
	@Override
	public void putAll(Map<? extends String, ? extends Profile> map)
	{
		profileMap.putAll(map);
	}

	/**
     * Removes the key (and its corresponding value) from this map.
     * This method does nothing if the key is not in the map.
     *
     * @param  key the key that needs to be removed
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
     * @throws NullPointerException if the specified key is null
     */
	@Override
	public Profile remove(Object key)
	{
		return profileMap.remove(key);
	}
	
	/**
     * {@inheritDoc}
     *
     * @throws NullPointerException if the specified key is null
     */
	@Override
	public boolean remove(Object key, Object value)
	{
		return profileMap.remove(key, value);
	}

	/**
     * {@inheritDoc}
     *
     * @return the previous value associated with the specified key,
     *         or <tt>null</tt> if there was no mapping for the key
     * @throws NullPointerException if the specified key or value is null
     */
	@Override
	public Profile replace(String key, Profile game)
	{
		return profileMap.replace(key, game);
	}

	/**
     * {@inheritDoc}
     *
     * @throws NullPointerException if any of the arguments are null
     */
	@Override
	public boolean replace(String key, Profile oldValue, Profile newValue)
	{
		return profileMap.replace(key, oldValue, newValue);
	}
	
	
	private static ConcurrentHashMap<String, Profile> profileMap;
}
